{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# [位姿,线性变换与坐标变换.yuque](https://www.yuque.com/wangpengfei-kgu2c/vnv7qd/ci63siah3eliqe1d?singleDoc)\n",
    "\n",
    "  因ggb网站添加了跨域限制,本地web代理的方式不能用了,这里用虚拟串口和浏览器通讯\n",
    "\n",
    "  [geogebra.yuque](https://qg543m.yuque.com/kz295v/lcgkg9/nogl1c?singleDoc)\n",
    "\n",
    "  [ggb指令文档](https://wiki.geogebra.org/zh/Category:%E6%8C%87%E4%BB%A4)\n",
    "  \n",
    "P1： 本地配置好虚拟串口\n",
    "\n",
    "P1:  打开 [ggb地址](https://www.geogebra.org/calculator/rkevrecw)\n",
    "\n",
    "P2:  在控制台注入WebSerialPlugin.js,点击连接串口即可\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "COM18 open success\n"
     ]
    }
   ],
   "source": [
    "from Ggb import Ggb\n",
    "import numpy as np\n",
    "from numpy import mat as m\n",
    "import math\n",
    "from math import cos,sin,pi\n",
    "ggb= Ggb(18)\n",
    "\n",
    "# 旋转\n",
    "def R(θ,x=0,y=0):\n",
    "   θ=math.radians(θ)\n",
    "   return  m([[cos(θ),-sin(θ),(1-cos(θ))*x+sin(θ)*y],\n",
    "              [sin(θ),cos(θ),-sin(θ)*x+(1-cos(θ))*y],\n",
    "              [0,     0,     1]\n",
    "      ])\n",
    "\n",
    "\n",
    "# 平移\n",
    "def T(x,y):\n",
    "   return  m([[1,0,      x],\n",
    "             [ 0,   1,    y],\n",
    "             [0,  0,     1]\n",
    "      ])\n",
    "\n",
    "# 点\n",
    "def p(x,y):\n",
    "    return m([x,y,1]).T\n",
    "\n",
    "# 将M作用到pName上\n",
    "def F(M,pName):\n",
    "    x,y=ggb.getCoord(pName)\n",
    "    b=M@p(x,y)\n",
    "    bX,bY= b[0,0],b[1,0]\n",
    "    ggb.setCoords(pName,bX,bY)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A复位到(0,0)\n",
    "ggb.setCoords(\"A\",0,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-4, 8)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看A的当前位置\n",
    "ggb.getCoord(\"A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#xA,yA=ggb.getCoord(\"A\")\n",
    "M=R(30)\n",
    "#M=T(1,1)\n",
    "F(M,\"A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加1个点 F\n",
    "ggb.evalCommand('F=(5,6)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 过F❀半径3的原\n",
    "ggb.evalCommand('Circle(F,3)')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# (3,3)圆心 半径f((3,3),(7,7))\n",
    "ggb.evalCommand('Circle((3,3),(7,7))')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有名函数\n",
    "# ggb.evalCommand('g1:y=x+5')\n",
    "# 设置函数颜色\n",
    "ggb.evalCommand('SetColor(g1,255,0,0)')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "ggb.evalCommand('g1:y=3sin(x)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "ggb.evalCommand('Line((3,5),(5,10))')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 折线\n",
    "ggb.evalCommand('Polyline({(1,1),(2,4),(2,5),(4,3),(3,6)})')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
